﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;
using DevComponents.DotNetBar.SuperGrid;

namespace SABI
{
    public partial class frm0092 : Form
    {
        DataTable dt1 = new DataTable();
        IGB igb = new IGB(FRM.ConnectString);
        bool _isNew = true;
        string sql = "";
        string _ObjectID;
        bool _isLoad = false;

        public frm0092()
        {
            InitializeComponent();
        }

        public frm0092(bool isNew = true, bool isView=false, string ObjectID = "")
        {
            InitializeComponent();
            _isNew = isNew;
            btnSave.Visible = !isView;
            txtObjectID.Enabled = isNew;
            btnAdd.Visible = isNew;
            _ObjectID = ObjectID;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frm0092_Load(object sender, EventArgs e)
        {

        }

        private void frm0092_Shown(object sender, EventArgs e)
        {
            GridComboBoxExEditControl gcb = (GridComboBoxExEditControl)GridDB1.PrimaryGrid.Columns["clSex"].EditControl;
            gcb.Items.Add("Nam");
            gcb.Items.Add("Nữ");
            loadDATA_Combo();
            if (!_isNew) 
            { 
                loadDATA_Edit(); 
                loadDATA_Grid1(); 
            }
        }

        void loadDATA_Grid1()
        {
            sql = string.Format("SELECT * FROM MK0003 WHERE ObjectID='{0}'", _ObjectID);
            GridDB1.PrimaryGrid.DataSource = FRM.GetDataTable(sql);

        }

        void loadDATA_Edit()
        {
            sql = string.Format("SELECT * FROM tb0045 WHERE ObjectID='{0}'", _ObjectID);
            DataTable dtT = FRM.GetDataTable(sql);
            if(dtT==null)
            {
                DLL02.Info("Dữ liệu đã bị xóa hoặc không tồn tại");
                Close();
                return;
            }
            TDBCboSTypeIDO01.SelectedValue = dtT.Rows[0]["S1"].ToString();
            TDBCboSTypeIDO02.SelectedValue = dtT.Rows[0]["S2"].ToString();
            TDBCboSTypeIDO03.SelectedValue = dtT.Rows[0]["S3"].ToString();
            txtObjectID.Text = dtT.Rows[0]["ObjectID"].ToString();
            txtObjectName.Text = dtT.Rows[0]["ObjectName"].ToString();
            txtTradeName.Text = dtT.Rows[0]["TradeName"].ToString();
            TDBCboObjectTypeID.SelectedValue = dtT.Rows[0]["ObjectTypeID"].ToString();
            txtVATNo.Text = dtT.Rows[0]["VATNo"].ToString();
            txtAddress.Text = dtT.Rows[0]["Address"].ToString();
            txtTel.Text = dtT.Rows[0]["Tel"].ToString();
            txtFax.Text = dtT.Rows[0]["Fax"].ToString();
            txtEmail.Text = dtT.Rows[0]["Email"].ToString();
            txtWebsite.Text = dtT.Rows[0]["Website"].ToString();
            txtContactor.Text = dtT.Rows[0]["Contactor"].ToString();
            txtMobile.Text = dtT.Rows[0]["Mobile"].ToString();
            txtContactor2.Text = dtT.Rows[0]["Contactor2"].ToString();
            txtMobile2.Text = dtT.Rows[0]["Mobile2"].ToString();
            TDBCboCityID.SelectedValue = dtT.Rows[0]["CityID"].ToString();
            TDBCboCountryID.SelectedValue = dtT.Rows[0]["CountryID"].ToString();
            chkIsSupplier.Checked = dtT.Rows[0]["IsSupplier"].ToString() == "1" ? true : false;
            chkIsSupplier_CheckedChanged(null, null);
            chkIsCustomer.Checked = dtT.Rows[0]["IsCustomer"].ToString() == "1" ? true : false;
            chkIsCustomer_CheckedChanged(null, null);
            chkIsUpdateName.Checked = dtT.Rows[0]["IsUpdateName"].ToString() == "1" ? true : false;
            chkDisabled.Checked = dtT.Rows[0]["Disabled"].ToString() == "1" ? true : false;
            TDBCboCurrencyID.SelectedValue = dtT.Rows[0]["CurrencyID"].ToString();
            txtReCreditLimit.Text = dtT.Rows[0]["ReCreditLimit"].ToString();
            TDBCboRePaymentTermID.SelectedValue = dtT.Rows[0]["RePaymentTermID"].ToString();
            txtReDueDays.Text = dtT.Rows[0]["ReDueDays"].ToString();
            txtDeAddress.Text = dtT.Rows[0]["DeAddress"].ToString();
            txtReDays.Text = dtT.Rows[0]["ReDays"].ToString();
            TDBCboReAccountID.Text = dtT.Rows[0]["ReAccountID"].ToString();
            txtDeAddress.Text = dtT.Rows[0]["DeAddress"].ToString();
            chkIsLockedOver.Checked = dtT.Rows[0]["IsLockedOver"].ToString() == "1" ? true : false;
            txtPaCreditLimit.Text = dtT.Rows[0]["PaCreditLimit"].ToString();
            TDBCboPaPaymentTermID.SelectedValue = dtT.Rows[0]["PaPaymentTermID"].ToString();
            txtPaDueDays.Text = dtT.Rows[0]["PaDueDays"].ToString();
            txtReAddress.Text = dtT.Rows[0]["ReAddress"].ToString();
            txtPaDiscountDays.Text = dtT.Rows[0]["PaDiscountDays"].ToString();
            txtPaDiscountPercent.Text = dtT.Rows[0]["PaDiscountPercent"].ToString();
            TDBCboPaAccountID.SelectedValue = dtT.Rows[0]["PaAccountID"].ToString();
            txtBankName.Text = dtT.Rows[0]["BankName"].ToString();
            txtBankAccountNo.Text = dtT.Rows[0]["BankAccountNo"].ToString();
            txtLicenseNo.Text = dtT.Rows[0]["LicenseNo"].ToString();
            txtLegalCapital.Text = dtT.Rows[0]["LegalCapital"].ToString();
            txtNote.Text = dtT.Rows[0]["Note"].ToString();
            txtNote1.Text = dtT.Rows[0]["Note1"].ToString();
            TDBCboAreaID.SelectedValue = dtT.Rows[0]["AreaID"].ToString();
            TDBCboAnaID1.SelectedValue = dtT.Rows[0]["O01ID"].ToString();
            TDBCboAnaID2.SelectedValue = dtT.Rows[0]["O02ID"].ToString();
            TDBCboAnaID3.SelectedValue = dtT.Rows[0]["O03ID"].ToString();
            TDBCboAnaID4.SelectedValue = dtT.Rows[0]["O04ID"].ToString();
            TDBCboAnaID5.SelectedValue = dtT.Rows[0]["O05ID"].ToString();
        }

        void loadDATA_Combo()
        {
            sql = "Select S, Sname From tb0050 Where Disabled=0 and StypeID='O01' Order By S";
            TDBCboSTypeIDO01.DataSource = DATA.GetDataTable(sql,FRM.ConnectString);

            sql = "Select S, Sname From tb0050 Where Disabled=0 and StypeID='O02' Order By S";
            TDBCboSTypeIDO02.DataSource = FRM.GetDataTable(sql);

            sql = "Select S, Sname From tb0050 Where Disabled=0 and StypeID='O03' Order By S";
            TDBCboSTypeIDO03.DataSource = FRM.GetDataTable(sql);

            // load dữ liệu mặc định cho 3 combobox trên
            sql = "Select * From tb0002 Where TableID='tb0045' and IsAutomatic <> 0";
            dt1 = FRM.GetDataTable(sql);
            TDBCboSTypeIDO01.SelectedValue = dt1.Rows[0]["S1"].ToString();
            TDBCboSTypeIDO02.SelectedValue = dt1.Rows[0]["S2"].ToString();
            TDBCboSTypeIDO03.SelectedValue = dt1.Rows[0]["S3"].ToString();            

            if(dt1.Rows[0]["IsAutomatic"].ToString()=="1" && _isNew)
            {
                lblSTypeID.Enabled = true;
                lblObjectID.Enabled = true;
                if(dt1.Rows[0]["IsS1"].ToString()=="1")
                {
                    lblSTypeIDO01.Enabled = true;
                    TDBCboSTypeIDO01.Enabled = true;
                }
                if (dt1.Rows[0]["IsS2"].ToString() == "1")
                {
                    lblSTypeIDO02.Enabled = true;
                    TDBCboSTypeIDO02.Enabled = true;
                }
                if (dt1.Rows[0]["IsS3"].ToString() == "1")
                {
                    lblSTypeIDO03.Enabled = true;
                    TDBCboSTypeIDO03.Enabled = true;
                }
                
            }
            if (_isNew) { loadDATA_ObjectID(); }

            //Phân loại khách hàng
            sql = "Select * from tb0044 Order by ObjectTypeID";
            TDBCboObjectTypeID.DataSource = FRM.GetDataTable(sql);

            sql = "Select * from tb0022 Order by CityID ";
            TDBCboCityID.DataSource = FRM.GetDataTable(sql);

            // Loại tiền giao dịch
            sql = "Select  CurrencyID,CurrencyName From tb0024 where Disabled=0 Order by CurrencyID";
            TDBCboCurrencyID.DataSource = FRM.GetDataTable(sql);

            // Điều khoản thanh toán THU
            sql = "Select PaymentTermID,PaymentTermName,DueDays From tb0051 Where Disabled =0 And IsDueDate=1";
            TDBCboRePaymentTermID.DataSource = FRM.GetDataTable(sql);

            // Tài khoản thu
            sql = "Select * From tb0025 Where Disabled =0 and IsNotShow =0 and GroupID not in ('G00') Order by AccountID";
            DataTable dt2 = FRM.GetDataTable(sql);
            TDBCboReAccountID.DataSource = dt2;
            TDBCboPaAccountID.DataSource = dt2;

            // Điều khoản thanh toán TRẢ
            sql = "Select PaymentTermID,PaymentTermName,DueDays,DiscountPercentage,DiscountDays From tb0051 Where";
            sql += " Disabled=0 And IsDueDate=1 Or IsDiscount=1";
            TDBCboPaPaymentTermID.DataSource = FRM.GetDataTable(sql);

            // Quốc gia
            sql = "Select * from tb0021 Order by CountryID";
            TDBCboCountryID.DataSource = FRM.GetDataTable(sql);

            // Khu vực
            sql = "Select AreaID,AreaName From tb0023 Where Disabled=0";
            TDBCboAreaID.DataSource = FRM.GetDataTable(sql);

            // Mã phân tích
            sql = "Select  *  From tb0033 Where AnaTypeID='O01' Order By AnaID";
            TDBCboAnaID1.DataSource = FRM.GetDataTable(sql);
            sql = "Select  *  From tb0033 Where AnaTypeID='O02' Order By AnaID";
            TDBCboAnaID2.DataSource = FRM.GetDataTable(sql);
            sql = "Select  *  From tb0033 Where AnaTypeID='O03' Order By AnaID";
            TDBCboAnaID3.DataSource = FRM.GetDataTable(sql);
            sql = "Select  *  From tb0033 Where AnaTypeID='O04' Order By AnaID";
            TDBCboAnaID4.DataSource = FRM.GetDataTable(sql);
            sql = "Select  *  From tb0033 Where AnaTypeID='O05' Order By AnaID";
            TDBCboAnaID5.DataSource = FRM.GetDataTable(sql);

            sql = "Select * From tb0005 Where TypeID like 'O%'";
            DataTable dt3 = FRM.GetDataTable(sql);

            if (dt3 != null)
            {
                lblAnaID1.Text = dt3.Rows[0]["UserName"].ToString();
                lblAnaID2.Text = dt3.Rows[1]["UserName"].ToString();
                lblAnaID3.Text = dt3.Rows[2]["UserName"].ToString();
                lblAnaID4.Text = dt3.Rows[3]["UserName"].ToString();
                lblAnaID5.Text = dt3.Rows[4]["UserName"].ToString();
            }

        }

        void loadDATA_ObjectID()
        {
            if (!lblSTypeID.Enabled) { return; }
            
            igb.TableName = "tb0045";
            igb.OutputOrder = (IGB.IGEOutputOrder)int.Parse(dt1.Rows[0]["OutputOrder"].ToString());
            igb.OutputLength = int.Parse(dt1.Rows[0]["Length"].ToString());
            if (lblSTypeIDO01.Enabled)
                igb.StringKey1 = TDBCboSTypeIDO01.Text;
            else
                igb.StringKey1 = "";

            if (lblSTypeIDO02.Enabled)
                igb.StringKey2 = TDBCboSTypeIDO02.Text;
            else
                igb.StringKey2= "";

            if (lblSTypeIDO03.Enabled)
                igb.StringKey3 = TDBCboSTypeIDO03.Text;
            else
                igb.StringKey3 = "";
            
            igb.SeperatorEnabled = dt1.Rows[0]["IsSeparator"].ToString() == "1" ? true : false;
            if (igb.SeperatorEnabled)
                igb.SeperatorCharacter = dt1.Rows[0]["Separator"].ToString();
            else
                igb.SeperatorCharacter = "";
            if(igb.StringKey1.Length+igb.StringKey2.Length+igb.StringKey3.Length+3*igb.SeperatorCharacter.Length>=igb.OutputLength)
            {
                DLL02.Alert("Mã đối tượng không vượt quá giới hạn độ dài cho phép!");
                return;
            }
            else
            {
                txtObjectID.Text = igb.Generate();
            }
        }

        bool Check_Input()
        {
            if (txtObjectID.Text.Trim() == "")
            {
                DLL02.Alert("Mã đối tượng không để trống!");
                return false;
            }
            else if(txtObjectName.Text.Trim()=="")
            {
                DLL02.Alert("Tên đối tượng không để trống!");
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!Check_Input()) { return; }

            if (txtObjectID.Enabled)
            {
                SaveDATA();
            }
            else
            {
                UpdateDATA();
            }
            

        }

        void UpdateDATA()
        {
            sql =  " Update tb0045 Set  ";
            sql += " ObjectName  = N'" + txtObjectName.Text + "',";
            sql += " IsSupplier  = '" + (chkIsSupplier.Checked?"1":"0") + "',";
            sql += " IsCustomer  = '" + (chkIsCustomer.Checked?"1":"0") + "',";
            sql += " IsUpdateName  = '" + (chkIsUpdateName.Checked?"1":"0") + "',";
            sql += " TradeName  = N'" + txtTradeName.Text + "',";
            sql += " ObjectTypeID = '" + TDBCboObjectTypeID.Text  + "',";
            sql += " Address  = N'" + txtAddress.Text + "',";
            sql += " CountryID = '" + TDBCboCountryID.Text + "',";
            sql += " CityID = '" + TDBCboCityID.Text + "',";
            sql += " Tel  = '" + txtTel.Text + "',";
            sql += " Mobile  = '" + txtMobile.Text + "',";
            sql += " Mobile2  = '" + txtMobile2.Text + "',";
            sql += " Fax  = '" + txtFax.Text + "',";
            sql += " Email  = '" + txtEmail.Text + "',";
            sql += " Website  = '" + txtWebsite.Text + "',";
            sql += " Contactor  = N'" + txtContactor.Text + "',";
            sql += " Contactor2  = N'" + txtContactor2.Text + "',";
            sql += " VATNo  = '" + txtVATNo.Text + "',";
            sql += " Note  = N'" + txtNote.Text + "',";
            sql += " Note1  = N'" + txtNote1.Text + "',";
            sql += " LastModifyUserID = '" + FRM.UserName + "', LastModifyDate = GetDate(), ";
            sql += " Disabled = '" + (chkDisabled.Checked?"1":"0") + "',";

            sql += " CurrencyID= '" + TDBCboCurrencyID.Text + "',";
            sql += " ReCreditLimit= '" + txtReCreditLimit.Text + "',";
            sql += " RePaymentTermID= '" + TDBCboRePaymentTermID.Text + "',";
            sql += " ReDueDays= '" + txtReDueDays.Text + "',";
            sql += " PaPaymentTermID= '" + TDBCboPaPaymentTermID.Text + "',";
            sql += " PaCreditLimit= '" + txtPaCreditLimit.Text+ "',";
            sql += " PaDueDays= '" + txtPaDueDays.Text + "',";
            sql += " PaDiscountDays= '" + txtPaDiscountDays.Text + "',";
            sql += " PaDiscountPercent= '" + txtPaDiscountPercent.Text + "',";
            sql += " AreaID= '" + TDBCboAreaID.Text + "',";
            sql += " ReAddress= N'" + txtReAddress.Text + "',";
            sql += " DeAddress= N'" + txtDeAddress.Text + "',";
            sql += " ReDays= '" + txtReDays.Text + "',";
            sql += " IsLockedOver = '" + (chkIsLockedOver.Checked?"1":"0") + "',";
            sql += " ReAccountID = '" + TDBCboReAccountID.Text + "',";
            sql += " PaAccountID = '" + TDBCboPaAccountID.Text + "',";
            sql += " BankName= N'" + txtBankName.Text + "',";
            sql += " BankAccountNo= '" + txtBankAccountNo.Text + "',";
            sql += " LicenseNo= '" + txtLicenseNo.Text+ "',";
            sql += " O01ID= '" + TDBCboAnaID1.Text + "',";
            sql += " O02ID= '" + TDBCboAnaID1.Text + "',";
            sql += " O03ID= '" + TDBCboAnaID1.Text + "',";
            sql += " O04ID= '" + TDBCboAnaID1.Text + "',";
            sql += " O05ID= '" + TDBCboAnaID1.Text + "',";
            sql += " LegalCapital= '" + txtLegalCapital.Text + "'";
            sql += " Where ObjectID = '" + txtObjectID.Text + "' ";

            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
            {
                DLL02.Info("Cập nhật dữ liệu thành công!");
                _isLoad = true;
                SaveDATA_Grid();
                Close();
            }
            else
            {
                DLL02.Alert("Cập nhật dữ liệu không thành công!");
            }
        }

        void SaveDATA_Grid()
        {
            sql = string.Format("DELETE MK0003 WHERE ObjectID='{0}'", txtObjectID.Text);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            GridDB1.Update();
            if (GridDB1.PrimaryGrid.Rows.Count < 2) { return; }
            for(int i=0; i<GridDB1.PrimaryGrid.Rows.Count-1;i++)
            {
                GridRow gr = (GridRow)GridDB1.PrimaryGrid.Rows[i];
                if (gr.Cells["clFullName"].Value==null)
                {
                    //TabControl92.SelectedIndex = 3;
                    // DLL02.Alert("Tên không để trống!","Thông báo");
                    GridDB1.PrimaryGrid.Rows.RemoveAt(i);
                    i--;
                }
            }

            for (int i = 0; i < GridDB1.PrimaryGrid.Rows.Count - 1; i++)
            {
                GridRow gr = (GridRow)GridDB1.PrimaryGrid.Rows[i];
                bool TempKey = false;
                while (!TempKey)
                {
                    IGB ig = new IGB(FRM.ConnectString);
                    ig.TableName = "MK0003";
                    ig.StringKey1 = "AV";
                    ig.StringKey2 = FRM.Year;
                    ig.StringKey3 = "";
                    ig.OutputOrder = IGB.IGEOutputOrder.lmSSSN;
                    ig.OutputLength = 16;
                    gr.Cells["clContactID"].Value = ig.Generate();
                    ig.SaveNewKey();
                    TempKey = !DLL01.GetID("MK0003", "ContactID", gr.Cells["clContactID"].Value.ToString(), "", FRM.ConnectString);
                }
                sql = "Insert Into MK0003(ContactID,ObjectID,FullName,Sex,PositionName,DepartmentName,Mobile,Tel,Email,Notes01,Notes02,Notes03)";
                sql += "Values(";
                sql += "'" + gr.Cells["clContactID"].Value.ToString() + "',";
                sql += "'" + txtObjectID.Text + "',";
                sql += "N'" + gr.Cells["clFullName"].Value.ToString() + "',";
                sql += "'" + (gr.Cells["clSex"].Value == null ? "" : gr.Cells["clSex"].Value.ToString()) + "',";
                sql += "N'" + (gr.Cells["clPositionName"].Value == null ? "" : gr.Cells["clPositionName"].Value.ToString()) + "',";
                sql += "N'" + (gr.Cells["clDepartmentName"].Value == null ? "" : gr.Cells["clDepartmentName"].Value.ToString()) + "',";
                sql += "'" + (gr.Cells["clMobile"].Value == null ? "" : gr.Cells["clMobile"].Value.ToString()) + "',";
                sql += "'" + (gr.Cells["clTel"].Value == null ? "" : gr.Cells["clTel"].Value.ToString()) + "',";
                sql += "'" + (gr.Cells["clEmail"].Value == null ? "" : gr.Cells["clEmail"].Value.ToString()) + "',";
                sql += "N'" + (gr.Cells["clNotes01"].Value == null ? "" : gr.Cells["clNotes01"].Value.ToString()) + "',";
                sql += "N'" + (gr.Cells["clNotes02"].Value == null ? "" : gr.Cells["clNotes02"].Value.ToString()) + "',";
                sql += "N'" + (gr.Cells["clNotes03"].Value == null ? "" : gr.Cells["clNotes03"].Value.ToString()) + "')";
                DATA.ExecuteNonQuery(sql, FRM.ConnectString);      
            }
            

        }

        void SaveDATA()
        {
            sql = " Insert Into tb0045( ObjectID ,ObjectName, DivisionID, IsSupplier, IsCustomer, IsUpdateName, TradeName, ObjectTypeID , ";
            sql += " Address, CountryID, CityID, Tel,Mobile,Mobile2,Fax, Email,Website,Contactor,Contactor2,VATNo, Note, Note1,";
            sql += " Disabled,CreateUserID,CreateDate,LastModifyUserID,LastModifyDate,S1,S2,S3, O01ID, O02ID, O03ID, O04ID, O05ID,CurrencyID,ReCreditLimit,RePaymentTermID,ReDueDays,";
            sql += " PaCreditLimit,PaPaymentTermID,PaDueDays,PaDiscountDays,PaDiscountPercent,";
            sql += " ReAddress,DeAddress,ReDays,IsLockedOver, ReAccountID, PaAccountID, ";
            sql += " BankName,BankAccountNo,LicenseNo,LegalCapital,AreaID,GroupShow) Values( ";
            sql += "'" + txtObjectID.Text + "',N'" + txtObjectName.Text + "', '" + FRM.DivisionID + "', " ;
            sql += "" + (chkIsSupplier.Checked ? "1" : "0") + ", " + (chkIsCustomer.Checked ? "1" : "0") + "," + (chkIsUpdateName.Checked ? "1" : "0") + ",";
            sql += "N'" + txtTradeName.Text + "',";
            sql += "'" + TDBCboObjectTypeID.Text + "',";
            sql += "N'" + txtAddress.Text + "',";
            sql += "'" + TDBCboCountryID.Text + "',";
            sql += "'" + TDBCboCityID.Text + "',";
            sql += "'" + txtTel.Text + "',";
            sql += "'" + txtMobile.Text + "',";
            sql += "'" + txtMobile2.Text + "',";
            sql += "'" + txtFax.Text + "',";
            sql += "'" + txtEmail.Text + "',";
            sql += "'" + txtWebsite.Text + "',";
            sql += "N'" + txtContactor.Text + "',";
            sql += "N'" + txtContactor2.Text + "',";
            sql += "'" + txtVATNo.Text + "',";
            sql += "N'" + txtNote.Text + "',";
            sql += "N'" + txtNote1.Text + "',";

            sql += " 0 , '" + FRM.UserName + "', Getdate() , '" + FRM.UserName + "', Getdate(),";
            sql += "'" + TDBCboSTypeIDO01.Text + "',";
            sql += "'" + TDBCboSTypeIDO02.Text + "',";
            sql += "'" + TDBCboSTypeIDO03.Text + "',";

            sql += "'" + TDBCboAnaID1.Text + "',";
            sql += "'" + TDBCboAnaID2.Text + "',";
            sql += "'" + TDBCboAnaID3.Text + "',";
            sql += "'" + TDBCboAnaID4.Text + "',";
            sql += "'" + TDBCboAnaID5.Text + "',";

            sql += "'" + TDBCboCurrencyID.Text + "',";
            sql += "'" + txtReCreditLimit.Text + "',";
            sql += "'" + TDBCboRePaymentTermID.Text + "',";
            sql += "'" + txtReDueDays.Text + "',";
            sql += "'" + txtPaCreditLimit.Text + "',";
            sql += "'" + TDBCboPaPaymentTermID.Text + "',";
            sql += "'" + txtPaDueDays.Text + "',";
            sql += "'" + txtPaDiscountDays.Text + "',";
            sql += "'" + txtPaDiscountPercent.Text + "',";
            sql += "N'" + txtReAddress.Text + "',";
            sql += "N'" + txtDeAddress.Text + "',";
            sql += "'" + txtReDays.Text + "',";
            sql += "" + (chkIsLockedOver.Checked ? "1" : "0") + ",";

            sql += "'" + TDBCboReAccountID.Text + "',";
            sql += "'" + TDBCboPaAccountID.Text + "',";

            sql += "N'" + txtBankName.Text + "',";
            sql += "'" + txtBankAccountNo.Text + "',";
            sql += "'" + txtLicenseNo.Text + "',";
            sql += "'" + txtLegalCapital.Text + "',";
            sql += "'" + TDBCboAreaID.Text + "',";
            sql += " '" + FRM.ID_Group + "') ";

            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
            {
                DLL02.Info("Cập nhật dữ liệu thành công!");
                igb.SaveNewKey();
                lblSTypeID.Enabled = false;
                lblObjectID.Enabled = false;
                lblSTypeIDO01.Enabled = false;
                TDBCboSTypeIDO01.Enabled = false;
                lblSTypeIDO02.Enabled = false;
                TDBCboSTypeIDO02.Enabled = false;
                lblSTypeIDO03.Enabled = false;
                TDBCboSTypeIDO03.Enabled = false;
                txtObjectID.Enabled = false;
                _isLoad = true;
                SaveDATA_Grid();
            }
            else
            {
                DLL02.Alert("Cập nhật dữ liệu không thành công!");
            }
        }

        private void TDBCboSTypeIDO01_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDATA_ObjectID();
        }

        private void TDBCboSTypeIDO02_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDATA_ObjectID();
        }

        private void TDBCboSTypeIDO03_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDATA_ObjectID();
        }

        private void chkIsCustomer_CheckedChanged(object sender, EventArgs e)
        {
            chkPaymentReceive.Checked = chkIsCustomer.Checked;
            chkPaymentReceive.Enabled = chkIsCustomer.Checked;
            txtReCreditLimit.Enabled = chkIsCustomer.Checked;
            TDBCboRePaymentTermID.Enabled = chkIsCustomer.Checked;
            TDBCboRePaymentTermID.Text = "";
            TDBCboReAccountID.Enabled = chkIsCustomer.Checked;
            txtReDueDays.Enabled = chkIsCustomer.Checked;
            txtDeAddress.Enabled = chkIsCustomer.Checked;
            txtReDays.Enabled = chkIsCustomer.Checked;
            chkIsLockedOver.Checked = chkIsCustomer.Checked;
            chkIsLockedOver.Enabled = chkIsCustomer.Checked;
        }

        private void chkIsSupplier_CheckedChanged(object sender, EventArgs e)
        {
            chkPaymentPay.Checked = chkIsSupplier.Checked;
            chkPaymentPay.Enabled = chkIsSupplier.Checked;
            txtPaCreditLimit.Enabled = chkIsSupplier.Checked;
            TDBCboPaPaymentTermID.Enabled = chkIsSupplier.Checked;
            TDBCboPaPaymentTermID.Text = "";
            TDBCboPaAccountID.Enabled = chkIsSupplier.Checked;
            txtPaDueDays.Enabled = chkIsSupplier.Checked;
            txtPaDiscountDays.Enabled = chkIsSupplier.Checked;
            txtPaDiscountPercent.Enabled = chkIsSupplier.Checked;
            txtReAddress.Enabled = chkIsSupplier.Checked;
        }

        private void frm0092_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_isLoad) { (Application.OpenForms["frm0076"] as frm0076).loadDATA_Grid1(); }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            lblSTypeID.Enabled = true;
            lblObjectID.Enabled = true;
            lblSTypeIDO01.Enabled = true;
            TDBCboSTypeIDO01.Enabled = true;
            lblSTypeIDO02.Enabled = true;
            TDBCboSTypeIDO02.Enabled = true;
            lblSTypeIDO03.Enabled = true;
            TDBCboSTypeIDO03.Enabled = true;
            txtObjectID.Enabled = true;

            txtObjectID.Text = "";
            txtObjectName.Text = "";
            txtTradeName.Text = "";
            txtVATNo.Text = "";
            txtAddress.Text = "";
            txtTel.Text = "";
            txtFax.Text = "";
            txtEmail.Text = "";
            txtWebsite.Text = "";
            txtContactor.Text = "";
            txtContactor2.Text = "";
            txtMobile.Text = "";
            txtMobile2.Text = "";
            txtDeAddress.Text = "";
            txtReAddress.Text = "";
            txtBankName.Text = "";
            txtBankAccountNo.Text = "";
            txtLicenseNo.Text = "";
            txtLegalCapital.Text = "";
            txtNote.Text = "";
            txtNote1.Text = "";
            TDBCboAreaID.Text = "";
            TDBCboAnaID1.Text = "";
            TDBCboAnaID2.Text = "";
            TDBCboAnaID3.Text = "";
            TDBCboAnaID4.Text = "";
            TDBCboAnaID5.Text = "";
            GridDB1.PrimaryGrid.Rows.Clear();

            loadDATA_Combo();

        }

    }
}
